<?php

/**
 * uniBoard TASK: Send Notifications from E-Mail Queue
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class task{
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Task
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Check if queue is not empty
		if ( $appCore -> conf['mails_in_queue'])
		{
			// Number of mails sent
			$sent_mails = 0;
			
			// Sent Mails ID's
			$sent_mails_ids = array();
			
			// Start Mailer
			$mailer = new class_mailer( $appCore, true);
			
			// Select Emails
			$appCore -> db -> query( 'SELECT * FROM ' . DB_PREFIX . 'mail_queue LIMIT ' . $appCore -> conf['board_email_package_size']);
			
			// Get Em
			while ( $mail_result = $appCore -> db -> fetch_array())
			{
				// Increase Counter
				$sent_mails ++;
				
				// Add Mail to list of sent E-Mails
				$sent_mails_ids[] = $mail_result[0];
				
				// Send E-Mail
				$mailer -> sendEmail( $mail_result[1], $mail_result[2], $mail_result[3], $mail_result[4]);
			}
			
			// Free Result
			$appCore -> db -> freeResult();
			
			// Tell it to resynch stuff
			$mailer -> saveQueue();
			
			// Delete mails from queue?
			if ( $sent_mails > 0)
			{
				$appCore -> db -> delete( 'mail_queue', 'mail_id IN (' . join( ',', $sent_mails_ids) . ')');
			}
			
			// Queue empty?
			if ( $sent_mails < $appCore -> conf['board_email_package_size'])
			{
				$appCore -> setConfig( 'mails_in_queue', 0);
			}
		
		}
				
	}
	
}